<!--
 * @Description: 该组件可独立使用,详细传入参数可见prop    
 
                 最简单使用例如下：  

                 export default {
                    mounted() {
                               
                       let showMsg = (data) => {
                         console.log(data)
                       }
                       
                      //stomp
                       this.$FksWebsocket({
                         name: 'fks-ws',    //自定义名称，该名称需唯一    
                         url:"./api/sys-socket/server/websocket",
                         或者
                         url:"ws://localhost:9529/api/sys-socket/server/websocket",
                         destination:'/topic/message',
                         connectType:'stomp',
                         onNewMessage: showMsg,
                         onSuccess: showMsg,
                         onError: showMsg,
                         onClose:showMsg
                       })

                      //smqtt
                        this.$FksWebsocket({
                          name: 'fks-smqtt',
                          url: './api/sys-socket-smqtt/mqtt',
                          destination:'/topic/message',
                          connectType: 'smqtt',
                          onNewMessage: showMsg
                        })
                      },
            

                     //通过该方法创建的ws会全局存在并在store中存储，在不需要使用时请手动关闭，否则将全局存在
                     //退出到login登录页时会统一断开所有ws链接   
                     beforeDestroy() {   
                       let showCloseMsg = () => {
                         console.log('ws 关闭')
                       }
                       
                       //通过名称来断开ws链接
                       this.$FksWebsocket.close({
                         name: 'fks-ws', 
                         onClose: showCloseMsg
                       })

                       this.$FksWebsocket.close({
                         name: 'fks-smqtt', 
                         onClose: showCloseMsg
                       })
                     }
                   }      
 * @Version: 1.0
 * @Autor: chen_yt
 * @Date: 2020-11-02 17:15:31
 * @LastEditors: chen_yt
 * @LastEditTime: 2021-11-03 16:14:18
-->
<script>
import Connection from './Connection'

export default {
  name: 'ConnectPlugin',
  mixins: [Connection],
  data() {
    return {
      isFksServer: true,
      name: `websocket-${Math.random()}`,
      //链接地址。支持以相对地址构建，比如../api/server/xxx
      url: this.VUE_APP_SOCKET_API,
      //后端控制器的messagingTemplate.convertAndSendToUser中可设置推送地址地址，该地址与前端消息模板中的订阅地址一致
      //e：消息模板中订阅地址为'/topic/message',后端将消息推送到'/topic/message',前端订阅'/topic/message'
      destination: '',
      //链接建立方式，现在仅支持stomp及sqmtt
      connectType: 'stomp',
      //尝试链接等待5秒，连不上就断开
      connectPendingTime: 5000,
      //链接断开后的重连间隔
      reConnectInterval: 1000,
      maxReconnect: 15
    }
  },
  mounted() {
    // console.log('创建插件')
  },
  beforeDestroy() {
    // console.log('销毁插件')
  }
}
</script>

